﻿using BLevy.Entidad;
using BLevy.Negocio;
using Microsoft.Reporting.WinForms;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace BLevy.UI
{
    public partial class frmProvinciasReporte : Form
    {
        public frmProvinciasReporte()
        {
            InitializeComponent();
        }

        private void frmProvinciasReporte_Load(object sender, EventArgs e)
        {
            try
            {
                IList<Provincia> _provincias = Provincias.FindAll();
                DataTable _provinciasDt = ConvertToDataTable(_provincias);

                ReportDataSource rds = new ReportDataSource();
                rds = new ReportDataSource("DataSet1", _provinciasDt);

                this.reportViewer1.LocalReport.ReportEmbeddedResource = "BLevy.UI.Reportes.ProvinciasReporte.rdlc";

                this.reportViewer1.LocalReport.DataSources.Add(rds);
                this.reportViewer1.ZoomMode = Microsoft.Reporting.WinForms.ZoomMode.PageWidth;
                this.reportViewer1.RefreshReport();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }


        public DataTable ConvertToDataTable<T>(IList<T> data)
        {
            PropertyDescriptorCollection properties =
               TypeDescriptor.GetProperties(typeof(T));
            DataTable table = new DataTable();
            foreach (PropertyDescriptor prop in properties)
                table.Columns.Add(prop.Name, Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType);
            foreach (T item in data)
            {
                DataRow row = table.NewRow();
                foreach (PropertyDescriptor prop in properties)
                    row[prop.Name] = prop.GetValue(item) ?? DBNull.Value;
                table.Rows.Add(row);
            }
            return table;
        }


    }
}
